<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/19
 * Time: 16:06
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Service\Admin\ShopService;
use App\Constant\ErrorConstant;
use App\Rules\ShopRule;

class ShopController extends BaseController
{
    protected $service;

    public function __construct(Request $request, ShopService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 添加店铺
     * @return \Illuminate\Http\JsonResponse
     */
    public function addShop()
    {
        $validator  =   Validator::make($this->request->all(), [
            'shop_name'   =>    'required|max:50',
            'shop_contact'=>    'required|max:20|unique:mini_shop,shop_contact',
            'shop_email'  =>    'email',
            'shop_desc'   =>    'string',
            'shop_logo'   =>    'required|image|max:5120',
            'shop_pic'    =>    'required|image|max:5120',
            'province'    =>    'required|max:30',
            'city'        =>    'required|max:30',
            'area'        =>    'required|max:30',
            'address'     =>    'required|max:50',
            'is_live'     =>    'required|integer|between:0,1',
            'is_video'    =>    'required|integer|between:0,1',
            'short_video' =>    'required|integer|between:0,1',
            'auto_confirm'=>    'required|integer',
            'auto_cancel' =>    'required|integer',
            'auto_notify' =>    'required|integer',
            'sorting'     =>    'required|integer',
        ], [
            'shop_name.required'    =>  '店铺名称不能为空',
            'shop_contact.required' =>  '店铺联系方式不能为空',
            'shop_logo.required'    =>  '店铺LOGO不能为空',
            'shop_pic.required'     =>  '店铺封面图不能为空',
            'province.required'     =>  '店铺省份地址不能为空',
            'city.required'         =>  '店铺城市地址不能为空',
            'area.required'         =>  '店铺地区地址不能为空',
            'address.required'      =>  '店铺详细地址不能为空',
            'is_live.required'      =>  '是否开启直播不能为空',
            'is_video.required'     =>  '是否开始视频不能为空',
            'short_video.required'  =>  '是否开启短视频不能为空',
            'auto_confirm.required' =>  '订单自动确认收货时间不能为空',
            'auto_cancel.required'  =>  '订单自动取消时间不能为空',
            'auto_notify.required'  =>  '订单自动通知时间不能为空',
            'sorting.required'      =>  '店铺权重不能为空',

            'shop_name.max'         =>  '店铺名称过长',
            'shop_contact.max'      =>  '店铺联系方式过长',
            'province.max'          =>  '店铺省份地址过长',
            'city.max'              =>  '店铺城市地址过长',
            'area.max'              =>  '店铺地区地址过长',
            'address.max'           =>  '店铺详细地址过长',
            'shop_logo.max'         =>  '店铺LOGO过大',
            'shop_pic.max'          =>  '店铺封面图过大',

            'shop_contact.unique'   =>  '店铺联系方式已存在',
            'shop_email.email'      =>  '店铺邮箱地址格式错误',
            'shop_desc.string'      =>  '店铺描述必须为字符串',
            'shop_logo.image'       =>  '店铺LOGO必须为图片',
            'shop_pic.image'        =>  '店铺封面图必须为图片',

            'is_live.integer'       =>  '是否开启直播必须为整数',
            'is_video.integer'      =>  '是否开启视频必须为整数',
            'short_video.integer'   =>  '是否开启短视频必须为整数',
            'auto_confirm.integer'  =>  '订单自动确认收货时间必须为整数',
            'auto_cancel.integer'   =>  '订单自动取消时间必须为整数',
            'auto_notify.integer'   =>  '订单自动通知时间必须为整数',
            'sorting.integer'       =>  '店铺权重必须为整数',

            'is_live.between'       =>  '是否开启直播选择错误',
            'is_video.between'      =>  '是否开启视频选择错误',
            'short_video.between'   =>  '是否开启短视频选择错误'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_ADD_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->shopAdd($this->request->all());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_ADD_ERROR, $result);
        }
    }

    /**
     * 店铺列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listShop()
    {
        $validator  =   Validator::make($this->request->post(), [
            'query' =>  'array',
        ], [
            'query.array'   =>  '搜索条件必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_LiST_ERROR, $validator->errors()->first());
        }

        $query  =   $this->request->post('query', []);
        $page   =   (int) $this->request->input('page', 1);
        $size   =   (int) $this->request->input('page_size', 20);
        [$flag, $result]    =   $this->service->shopList($query, $page, $size);

        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_LiST_ERROR, $result);
        }
    }

    /**
     * 店铺详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function infoShop()
    {
        $validator  =   Validator::make($this->request->post(), [
            'shop_id'   =>  ['required', new ShopRule]
        ], [
            'shop_id.required' => '店铺ID不能为空'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_INFO_ERROR, $validator->errors()->first());
        }

        $shop_id   =   (int) $this->request->post('shop_id');
        [$flag, $result]    =   $this->service->shopInfo($shop_id);

        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_INFO_ERROR, $result);
        }
    }

    /**
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function editShop()
    {
        $validator  =   Validator::make($this->request->all(), [
            'shop_id'     =>    ['required', 'integer', new ShopRule],
            'shop_name'   =>    'required|max:50',
            'shop_contact'=>    'required|max:20|unique:mini_shop,shop_contact',
            'shop_email'  =>    'email',
            'shop_desc'   =>    'string',
            'shop_logo'   =>    'required|image|max:5120',
            'shop_pic'    =>    'required|image|max:5120',
            'province'    =>    'required|max:30',
            'city'        =>    'required|max:30',
            'area'        =>    'required|max:30',
            'address'     =>    'required|max:50',
            'is_live'     =>    'required|integer|between:0,1',
            'is_video'    =>    'required|integer|between:0,1',
            'short_video' =>    'required|integer|between:0,1',
            'auto_confirm'=>    'required|integer',
            'auto_cancel' =>    'required|integer',
            'auto_notify' =>    'required|integer',
            'sorting'     =>    'required|integer',
        ], [
            'shop_id.required'      =>  '店铺ID不能为空',
            'shop_name.required'    =>  '店铺名称不能为空',
            'shop_contact.required' =>  '店铺联系方式不能为空',
            'shop_logo.required'    =>  '店铺LOGO不能为空',
            'shop_pic.required'     =>  '店铺封面图不能为空',
            'province.required'     =>  '店铺省份地址不能为空',
            'city.required'         =>  '店铺城市地址不能为空',
            'area.required'         =>  '店铺地区地址不能为空',
            'address.required'      =>  '店铺详细地址不能为空',
            'is_live.required'      =>  '是否开启直播不能为空',
            'is_video.required'     =>  '是否开始视频不能为空',
            'short_video.required'  =>  '是否开启短视频不能为空',
            'auto_confirm.required' =>  '订单自动确认收货时间不能为空',
            'auto_cancel.required'  =>  '订单自动取消时间不能为空',
            'auto_notify.required'  =>  '订单自动通知时间不能为空',
            'sorting.required'      =>  '店铺权重不能为空',

            'shop_name.max'         =>  '店铺名称过长',
            'shop_contact.max'      =>  '店铺联系方式过长',
            'province.max'          =>  '店铺省份地址过长',
            'city.max'              =>  '店铺城市地址过长',
            'area.max'              =>  '店铺地区地址过长',
            'address.max'           =>  '店铺详细地址过长',
            'shop_logo.max'         =>  '店铺LOGO过大',
            'shop_pic.max'          =>  '店铺封面图过大',

            'shop_contact.unique'   =>  '店铺联系方式已存在',
            'shop_email.email'      =>  '店铺邮箱地址格式错误',
            'shop_desc.string'      =>  '店铺描述必须为字符串',
            'shop_logo.image'       =>  '店铺LOGO必须为图片',
            'shop_pic.image'        =>  '店铺封面图必须为图片',

            'shop_id.integer'       =>  '店铺ID必须为整数',
            'is_live.integer'       =>  '是否开启直播必须为整数',
            'is_video.integer'      =>  '是否开启视频必须为整数',
            'short_video.integer'   =>  '是否开启短视频必须为整数',
            'auto_confirm.integer'  =>  '订单自动确认收货时间必须为整数',
            'auto_cancel.integer'   =>  '订单自动取消时间必须为整数',
            'auto_notify.integer'   =>  '订单自动通知时间必须为整数',
            'sorting.integer'       =>  '店铺权重必须为整数',

            'is_live.between'       =>  '是否开启直播选择错误',
            'is_video.between'      =>  '是否开启视频选择错误',
            'short_video.between'   =>  '是否开启短视频选择错误'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_EDIT_ERROR, $validator->errors()->first());
        }

        $shop_id    =   (int) $this->request->post('shop_id');
        $params     =   $this->request->all();
        [$flag, $result]    =   $this->service->shopEdit($shop_id, $params);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_EDIT_ERROR, $result);
        }
    }

    /**
     * 店铺审核
     * @return \Illuminate\Http\JsonResponse
     */
    public function verifyShop()
    {
        $validator  =   Validator::make($this->request->post(), [
            'shop_id'   =>  ['required', 'integer', new ShopRule],
            'apply_res' =>  'required|integer|between:1,2',
            'apply_msg' =>  'string|max:500'
        ], [
            'shop_id.required'      => '店铺ID不能为空',
            'shop_id.integer'       => '店铺ID必须为整数',
            'apply_res.required'    =>  '审核结果不能为空',
            'apply_res.integer'     =>  '审核结果选择错误',
            'apply_res.between'     =>  '审核结果错误',
            'apply_msg.string'      =>  '审核结果信息必须为字符',
            'apply_msg.max'         =>  '审核结果过长'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_DELETE_ERROR, $validator->errors()->first());
        }
        $shop_id    =   (int) $this->request->post('shop_id');
        $apply_res  =   (int) $this->request->post('apply_res');
        $apply_msg  =   $this->request->post('apply_msg', '');

        [$flag, $result]    =   $this->service->shopVerify($shop_id, $apply_res, $apply_msg);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_VERIFY_ERROR, $result);
        }
    }

    /**
     * 店铺删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteShop()
    {
        $validator  =   Validator::make($this->request->post(), [
            'shop_id'   =>  ['required', 'integer', new ShopRule]
        ], [
            'shop_id.required'      => '店铺ID不能为空',
            'shop_id.integer'       => '店铺ID必须为整数',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_DELETE_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->shopDelete((int) $this->request->post('shop_id'));
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_DELETE_ERROR, $result);
        }
    }

    /**
     * 店铺锁定
     * @return \Illuminate\Http\JsonResponse
     */
    public function lockShop()
    {
        $validator  =   Validator::make($this->request->post(), [
            'shop_id'   =>  ['required', 'integer', new ShopRule]
        ], [
            'shop_id.required'      => '店铺ID不能为空',
            'shop_id.integer'       => '店铺ID必须为整数',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_LOCK_ERROR, $validator->errors()->first());
        }

        $shop_id    =   (int) $this->request->post('shop_id');
        [$flag, $result]    =   $this->service->shopLock($shop_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_SHOP_LOCK_ERROR, $result);
        }
    }

}
